﻿/*
 *  ADICIONADO NOVOS CAMPOS
 *
**/

ALTER TABLE  `tturmaalunoaprovado` 
ADD  `Verificado` 		TINYINT( 1 ) 	NOT NULL DEFAULT  '0' 		AFTER  `ID_Aluno`                      ,
ADD  `Data_Aprovado` 	DATE  		NOT NULL DEFAULT  '0000-00-00' 	AFTER  `EnviadoAluno`               ,
ADD  `Data_Verificado` 	DATE  		NOT NULL DEFAULT  '0000-00-00' 	AFTER  `Data_Aprovado`            ,
ADD  `Data_Remessa`         	DATE  		NOT NULL DEFAULT  '0000-00-00' 	AFTER  `Data_Verificado`            ,
ADD  `Responsavel_Verificado` 	INT 		NOT NULL DEFAULT  '0' 		AFTER  `Data_EnviadoAluno`      ,
ADD  `Responsavel_Remessa`  	INT 		NOT NULL DEFAULT  '0' 		AFTER  `Responsavel_Verificado`

/*
 *  ADICIONADO NOVOS CAMPOS
 *
**/
ALTER TABLE  `tturmaalunocertificado` 
ADD  `ID_Lote_Certificado` INT(11) UNSIGNED  NULL AFTER  `ID_Turma`     

/*
 * CRIADA NOVA TABELA AUXILIAR PARA NÃO PERMITIR DUPLICIDADE DE CERTIFCADO INFORMADO PELA INSTITUIÇÃO
 *
**/
CREATE TABLE IF NOT EXISTS `tturmaalunocertificado_aux` (
  `ID_Certificado` 	int(11) 	 NOT NULL,
  `nr_certificado` 	varchar(20)          NULL,
  `nr_livro` 		varchar(20)          NULL,
  `nr_folha` 		varchar(20)          NULL,
  PRIMARY KEY (`ID_Certificado`),
  UNIQUE KEY `livro_folha` (`nr_certificado`,`nr_livro`,`nr_folha`),
  KEY `nr_certificado` (`nr_certificado`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

ALTER TABLE `SIS_modelo`.`tturmaalunocertificado_aux` 
  ADD CONSTRAINT `FK_tturmaalunocertificado_aux_1` 
    FOREIGN KEY `FK_tturmaalunocertificado_aux_1` (`ID_Certificado`)
      REFERENCES `tturmaalunocertificado` (`ID_Certificado`)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT
, ROW_FORMAT = DYNAMIC;

/*
 * CRIADA NOVA TABELA AUXILIAR PARA IDENTIFICAR O TIPO DE DOCUMENTO EMITIDO
 *
**/
CREATE TABLE IF NOT EXISTS `tipo_documento` (
  `ID_Tipo_Documento` 	int(11) 	   NOT NULL 	AUTO_INCREMENT,
  `Descricao` 		varchar(100) NOT NULL,
  PRIMARY KEY (`ID_Tipo_Documento`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

INSERT INTO `tipo_documento` (`ID_Tipo_Documento`, `Descricao`) 
VALUES 	(1, 'Certificado'),
                	(2, 'Histórico Escolar'),
       	(3, 'Diário Escolar'),
	(4, 'Declaração'),
	(5, 'Contrato ');

/*
 * CRIADA NOVA TABELA PARA CONTROLE DE LOTE DE ALUNOS APROVADOS , PARA EMISSÃO DE CERTIFICADOS
 *
**/
CREATE TABLE IF NOT EXISTS `lote_documento` (
  `ID_Lote_Documento` 	int(11) 	unsigned 	NOT NULL 	AUTO_INCREMENT,
  `ID_Agente` 		int(11) 		NOT NULL,
  `data_hora` 		datetime 		NOT NULL,
  `nome_lote` 		varchar(100)	DEFAULT NULL,
  PRIMARY KEY (`ID_Lote_Documento`),
  KEY `FK_lote_documento_1` (`ID_Agente`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

ALTER TABLE `SIS_modelo`.`lote_documento` 
   ADD CONSTRAINT `FK_lote_documento_1` 
     FOREIGN KEY `FK_lote_documento_1` (`ID_Agente`)
       REFERENCES `tagente` (`ID_Agente`)
         ON DELETE RESTRICT
         ON UPDATE RESTRICT
, ROW_FORMAT = DYNAMIC;
/*
 * CRIADA NOVA TABELA AUXILIAR PARA ACOMPANHAR ALTERAÇÕES DE PROCEDIMENTOS
 *
**/
CREATE TABLE IF NOT EXISTS `lote_documento_log` (
  `ID_Lote_Documento_Log` 	int(11) 	unsigned 	NOT NULL 	AUTO_INCREMENT,
  `ID_Lote_Documento` 	int(11) 	unsigned 	NOT NULL,
  `ID_Agente` 		int(11) 		NOT NULL,
  `data_hora` 		datetime 		NOT NULL,
  `Acao` 			enum('E','R') 	NOT NULL 	COMMENT 'E = enviado , R = recebido',
  `Endereco_IP` 		int(11) 	unsigned 	NOT NULL,
  PRIMARY KEY (`ID_Lote_Documento_Log`) USING BTREE,
    KEY `FK_lote_documento_log_1` (`ID_Agente`),
    KEY `FK_lote_documento_log_2` (`ID_Lote_Documento`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

ALTER TABLE `lote_documento_log`
  ADD CONSTRAINT `FK_lote_documento_log_1` 
    FOREIGN KEY (`ID_Agente`) 		REFERENCES `tagente` (`ID_Agente`),
  ADD CONSTRAINT `FK_lote_documento_log_2` 
    FOREIGN KEY (`ID_Lote_Documento`) 	REFERENCES `lote_documento` (`ID_Lote_Documento`);

/*
 * CRIADA NOVA TABELA AUXILIAR PARA ACOMPANHAR ALTERAÇÕES DE PROCEDIMENTOS DETALHE POR REGISTRO
 *
**/
CREATE TABLE IF NOT EXISTS `lote_documento_log_det` (
  `ID_Log_Documento` 	int(11) 	unsigned 	NOT NULL 	AUTO_INCREMENT,
  `ID_Lote_Documento` 	int(11) 	unsigned 	NOT NULL,
  `ID_Agente` 		int(11) 		NOT NULL,
  `ID_Turma` 		int(11) 		NOT NULL,
  `ID_Aluno` 		int(11) 		NOT NULL,
  `ID_Tipo_Documento` 	int(11) 	      	NOT NULL,
  `data_hora` 		datetime 		NOT NULL,
  `Acao` 			enum('E','R','C') 	NOT NULL 	COMMENT 'E= enviado, R = recebido, C = cancelado',
  `Endereco_IP` 		int(11) 	unsigned 	NOT NULL,
  `Justificativa` 		text 		NOT NULL,
  PRIMARY KEY (`ID_Log_Documento`),
  KEY `FK_lote_documento_log_det_1` (`ID_Lote_Documento`),
  KEY `FK_lote_documento_log_det_2` (`ID_Agente`),
  KEY `FK_lote_documento_log_det_3` (`ID_Turma`),
  KEY `FK_lote_documento_log_det_4` (`ID_Aluno`),
  KEY `FK_lote_documento_log_det_5` (`ID_Tipo_Documento`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

ALTER TABLE `lote_documento_log_det`
  ADD CONSTRAINT `FK_lote_documento_log_det_1` 
    FOREIGN KEY (`ID_Lote_Documento`) REFERENCES `lote_documento` (`ID_Lote_Documento`),
  ADD CONSTRAINT `FK_lote_documento_log_det_2` 
    FOREIGN KEY (`ID_Agente`) REFERENCES `tagente` (`ID_Agente`),
  ADD CONSTRAINT `FK_lote_documento_log_det_3` 
    FOREIGN KEY (`ID_Turma`) REFERENCES `tturma` (`ID_Turma`),
  ADD CONSTRAINT `FK_lote_documento_log_det_4` 
    FOREIGN KEY (`ID_Aluno`) REFERENCES `taluno` (`ID_Aluno`),
  ADD CONSTRAINT `FK_lote_documento_log_det_5` 
    FOREIGN KEY (`ID_Tipo_Documento`) REFERENCES `tipo_documento` (`ID_Tipo_Documento`);
